//
//  AddViewController.m
//  MyNote
//
//  Created by Adiljan on 2016/11/16.
//  Copyright © 2016年 Adiljan. All rights reserved.
//

#import "AddViewController.h"


#define UISC_WIDTH [UIScreen mainScreen].bounds.size.width
#define UISC_HEIGHT [UIScreen mainScreen].bounds.size.height
#define txt_x 10
#define lblTime_y 10
#define lblTime_Height 30
#define INsert 5
#define APPNOte ((AppDelegate*)([UIApplication sharedApplication].delegate))
@interface AddViewController ()

@property (strong, nonatomic) UITextView *txtNote;
@property (strong, nonatomic) UILabel *lblDate;
@property (strong, nonatomic) NSString *strTime;

@property (nonatomic, strong) NSMutableArray *modalsArrM;

@end

@implementation AddViewController

-(NSMutableArray *)modalsArrM{
    if (!_modalsArrM) {
        _modalsArrM = [[NSMutableArray alloc]init];
        
    }
    return _modalsArrM;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initViews];
    [self initNavigation];
   
}
-(void)viewWillAppear:(BOOL)animated{
    
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.txtNote resignFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.txtNote resignFirstResponder];
}
- (void)initNavigation{
    UIAlmasButton *btnSave = [[UIAlmasButton alloc]initWithFrame:CGRectMake(0, 20, 60, 20)];
    [btnSave setTitle:NSLocalizedString(@"save", nil) forState:UIControlStateNormal];
    btnSave.titleLabel.font = FONTWITHSIZE(17);
    [btnSave addTarget:self action:@selector(btnSaveClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:btnSave];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIAlmasButton *backBtn = [UIAlmasButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 25, 25);
    [backBtn setImage:[UIImage imageNamed:@"back_right"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.rightBarButtonItem = backItem;
}
- (void)initViews{
    UIAlmasLabel *lblTitle = [[UIAlmasLabel alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
    lblTitle.font = FONTWITHSIZE(24);
    lblTitle.textColor = [UIColor whiteColor];
    lblTitle.text = NSLocalizedString(@"hatirem", nil);
    
    self.navigationItem.titleView = lblTitle;
    
    
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"abc.jpg"]];
    NSString *year = NSLocalizedString(@"year", nil);
    NSString *month = NSLocalizedString(@"month", nil);
    NSString *day = NSLocalizedString(@"day", nil);
    NSString *strDate = [NSString stringWithFormat:@"yyyy-%@ MM-%@ dd-%@ HH:mm",year,month,day];
    UILabel *lblDate = [[UILabel alloc]initWithFrame:CGRectMake(txt_x, lblTime_y, UISC_WIDTH-2*txt_x, lblTime_Height)];
    lblDate.textAlignment = NSTextAlignmentRight;
    lblDate.text = [self showNowTime:strDate];
    lblDate.backgroundColor = [UIColor clearColor];
    self.lblDate = lblDate;
    self.lblDate.font = FONTWITHSIZE(10);
    
    self.strTime = [self showNowTime:@"HH:mm:ss"];
    
    UITextView *txt = [[UITextView alloc]initWithFrame:CGRectMake(txt_x, lblTime_y+lblTime_Height+INsert, UISC_WIDTH-2*txt_x, UISC_HEIGHT-lblTime_y+lblTime_Height+INsert)];
    txt.backgroundColor = [UIColor clearColor];
    txt.showsHorizontalScrollIndicator = NO;
    txt.showsVerticalScrollIndicator = NO;
    [txt becomeFirstResponder];
    self.txtNote = txt;
    
    self.txtNote.font = FONTWITHSIZE(15);
    UyghurKeyboard *keyboard = [[UyghurKeyboard alloc]init];
    [self.txtNote setTextAlignment:NSTextAlignmentRight];
    [keyboard setTextView:self.txtNote];
    self.txtNote.text = nil;
    
    [keyboard.returnButton addTarget:self action:@selector(btnSaveClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:lblDate];
    [self.view addSubview:txt];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
    tap.numberOfTapsRequired = 1; //点击次数
    tap.numberOfTouchesRequired = 1; //点击手指数
    [self.view addGestureRecognizer:tap];
    // 让textview 的光标显示在顶部
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    if (self.fromType==typeUpdate) {
        self.txtNote.text = self.strContent;
        self.lblDate.text = self.strCreateDate;
    }
    
}
-(void)doBack:(UIAlmasButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSString *)showNowTime:(NSString *)timeFormat{
    NSDate *  senddate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:timeFormat];
    NSString *locationString=[dateformatter stringFromDate:senddate];
    
    return locationString;
}
//轻击手势触发方法
-(void)tapGesture:(UITapGestureRecognizer *)sender
{
    [self.txtNote resignFirstResponder];
}
/*Adiljan 保存*/
-(void)btnSaveClick{
    if (self.txtNote.text!=nil && ![self.txtNote.text isEqualToString:@""] && self.txtNote.text.length !=0) {
        NoteModel *model = [NoteModel modelWith:self.txtNote.text createDate:self.lblDate.text createTime:self.strTime];
        
        if (self.fromType == typeAdd) {// 添加
            BOOL isInsert = [NoteTools insertModel:model];
            
            if (isInsert) {
                NSLog(@"插入数据成功");
                [self.modalsArrM addObject:model];
                
            } else {
                NSLog(@"插入数据失败");
            }
        }else{// 修改
            NSString *strSql = [NSString stringWithFormat:@"UPDATE t_modals SET content = '%@' WHERE time = '%@'",self.txtNote.text,self.strCreateTime];
            BOOL modify = [NoteTools modifyData:strSql];
            if (modify) {
                NSLog(@"修改数据成功");
//                [self.modalsArrM addObject:model];
            } else {
                NSLog(@"修改数据失败");
            }
        }
        
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
